package com.aggregate.pay.weixin.support.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aggregate.pay.weixin.bean.RefundNotifyData;
import com.aggregate.pay.weixin.bean.RefundNotifyDataInfo;
import com.aggregate.pay.weixin.support.Client;
import com.aggregate.pay.weixin.support.HttpClient;
import com.aggregate.pay.weixin.support.Merch;
import com.aggregate.pay.weixin.support.utils.AES;
import com.aggregate.pay.weixin.support.utils.BASE64;
import com.aggregate.pay.weixin.support.utils.MD5;
import com.aggregate.pay.weixin.support.utils.XML;

/**
 * 微信支付官方接口-退款通知
 * @author Moyq5
 * @date 2018年7月6日
 */
public class RefundNotify implements Client<String, RefundNotifyData> {
	
	private static Logger log = LoggerFactory.getLogger(RefundNotify.class);
	
	private Merch merch;
	public RefundNotify(Merch merch) {
		this.merch = merch;
	}

	@Override
	public RefundNotifyData execute(String dataXml) {
		log.debug("微信退款通知内容：{}" + dataXml);
		RefundNotifyData data = XML.toObject(dataXml, RefundNotifyData.class);
		try {
			byte[] infoBytes = BASE64.decode(data.getReqInfo());
			String key = MD5.MD5Encode(merch.apiKey()).toLowerCase();
			String infoXml = AES.decrypt(infoBytes, key);
			RefundNotifyDataInfo info = XML.toObject(infoXml, RefundNotifyDataInfo.class);
			data.setDataInfo(info);
		} catch (Exception e) {
			log.error("微信退款通知内容解密失败", e);
		}
		return data;
	}

	@Override
	public void setHttpClient(HttpClient client) {
		log.debug("此接口无http请求，不需要httpClient对象");
	}

}
